<template>
  <y-dialog @open="open" @close="emit('close')" width="1000px" append-to-body>
    <el-form
      ref="activeBugRef"
      :model="form"
      :rules="rules"
      size="default"
      label-width="120px"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="开发接口人" prop="downer">
            <y-select
              v-model="form.downer"
              placeholder="请选择开发接口人"
              :optionsFunc="listUser"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label="描述" prop="description">
            <el-input
              v-model="form.description"
              type="textarea"
              :rows="5"
              placeholder="请输入描述信息"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button type="warning" @click="reset">重置</el-button>
        <el-button @click="emit('close')">取消</el-button>
      </div>
    </template>
  </y-dialog>
</template>

<script setup name="BugActive">
import { addBugOperate } from "@/api/tms/bug/operate";

const { proxy } = getCurrentInstance();
import { listUser } from "@/api/sys/user";

const emit = defineEmits(["submited", "close"]);

const data = reactive({
  form: {},
  rules: {
    downer: [
      { required: true, message: "开发接口人不能为空", trigger: "blur" },
    ],
  },
});

const { form, rules } = toRefs(data);

const props = defineProps({
  id: { type: String, default: undefined },
  getMethod: { type: Function, default: undefined },
  updateMethod: { type: Function, default: undefined },
});

/** 打开对话框事件 */
function open(){
  props.getMethod(props.id).then((res)=>{
    form.value.downer = res.data.downer;
  })
}

/** 重置Form方法 */
function reset() {
  form.value = {
    downer: undefined,
    description: "",
  };
}

/** 提交按钮事件 */
const submit = async () => {
  try {
    await proxy.$refs["activeBugRef"].validate();
    const bug = await props.getMethod(props.id);
    await props.updateMethod({
      ...bug.data,
      status: "activated",
      downer: form.value.downer,
    });
    await addBugOperate({
      bug: props.id,
      operation: "ACTIVATE",
      description: form.value.description,
    });
    proxy.$modal.msgSuccess("操作成功");
    submited();
  } catch (error) {
    console.error("操作失败:", error);
  }
};

/** submited事件 */
function submited() {
  emit("submited");
  emit("close");
}
</script>
